﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;


namespace RestaurantManagerWebApp.Class
{
    public class DBClass
    {

        public static string GetConnectionString()
        {
            return System.Configuration.ConfigurationManager.AppSettings["ConnectionString"];
        }


        public static DataTable getDataTable(String sSql, SqlParameterCollection sqlParams)
        {
            DataTable dt = new DataTable();
            try
            {
                using (SqlConnection con = new SqlConnection(GetConnectionString()))
                {
                    con.Open();

                    SqlCommand com = new SqlCommand(sSql, con);

                    foreach (SqlParameter sqlParam in sqlParams)
                    {
                        com.Parameters.AddWithValue(sqlParam.ParameterName, sqlParam.Value);
                    }

                    SqlDataAdapter da = new SqlDataAdapter(com);

                    da.Fill(dt);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return dt;
        }

        public static DataSet getDataSet(String sSql)
        {
            SqlConnection oConn;
            SqlCommand oCommand;
            SqlDataAdapter oDA;
            DataSet oDS = new DataSet();

            using (oConn = new SqlConnection(GetConnectionString()))
            {
                oConn.Open();

                oCommand = new SqlCommand(sSql, oConn);
                oDA = new SqlDataAdapter(oCommand);

                oDA.Fill(oDS);
                oConn.Close();
            }

            return oDS;
        }
    }
}
